﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.Unity;

namespace JTLeigh.Commerce.Spencer.Core.Common.Services
{

	/// <summary>
	/// An <see cref="ILoggingService"/> implementation that uses Enterprise Library's logging 
	/// application block to log messages.
	/// </summary>
	public class LoggingService : ILoggingService
	{

		#region Properties

		/// <summary>
		/// Gets or sets the exception handling service to use to handle exceptions.
		/// </summary>
		/// <value>An <see cref="IExceptionHandlingService"/> implementation.</value>
		[Dependency]
		public IExceptionHandlingService ExceptionHandlingService { get; set; }

		#endregion // Properties

		#region ILoggingService Members

		/// <summary>
		/// Logs the specified log entry.
		/// </summary>
		/// <param name="tenantId">The tenant identifier <see cref="Guid"/> of the tenant who the 
		/// message is to be logged for.</param>
		/// <param name="entry">The <see cref="LogEntry"/> to log.</param>
		public void Log(Guid tenantId, LogEntry entry)
		{
			Safety.AssertArgumentNotNull("entry", entry);

			try
			{
				Logger.Write(entry);
			}
			catch (Exception e)
			{
				// NOTE: This is one of the few places where we can safely swallow the exception.
				if (ExceptionHandlingService.HandleException(tenantId, e, "FIXME")) // FIXME: CoreExceptionPolicy.Logging
				{
					throw;
				}
			}
		}

		#endregion // ILoggingService Members

	}

}
